/*
 * UserForm2.java
 *
 * Created on February 8, 2008, 6:08 PM
 */

package com.floreantpos.ui.forms;

import java.util.List;

import javax.swing.DefaultComboBoxModel;

import com.floreantpos.PosException;
import com.floreantpos.model.User;
import com.floreantpos.model.UserType;
import com.floreantpos.model.dao.UserDAO;
import com.floreantpos.model.dao.UserTypeDAO;
import com.floreantpos.model.util.IllegalModelStateException;
import com.floreantpos.ui.BeanEditor;
import com.floreantpos.ui.dialog.POSMessageDialog;
import com.floreantpos.util.POSUtil;

/**
 * 
 * @author rodaya
 */
public class UserForm extends BeanEditor {

	/** Creates new form UserForm2 */
	public UserForm() {
		initComponents();

		UserTypeDAO dao = new UserTypeDAO();
		List<UserType> userTypes = dao.findAll();

		cbUserType.setModel(new DefaultComboBoxModel(userTypes.toArray()));
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {
		java.awt.GridBagConstraints gridBagConstraints;

		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel9 = new javax.swing.JLabel();
		jLabel10 = new javax.swing.JLabel();
		tfPassword1 = new javax.swing.JPasswordField();
		tfPassword2 = new javax.swing.JPasswordField();
		tfId = new javax.swing.JTextField();
		tfSsn = new javax.swing.JTextField();
		tfFirstName = new javax.swing.JTextField();
		tfLastName = new javax.swing.JTextField();
		jLabel5 = new javax.swing.JLabel();
		tfCostPerHour = new javax.swing.JTextField();
		jLabel6 = new javax.swing.JLabel();
		cbUserType = new javax.swing.JComboBox();

		setLayout(new java.awt.GridBagLayout());

		jLabel1.setText("ID" + ":");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
		add(jLabel1, gridBagConstraints);

		jLabel2.setText("SSN" + ":");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
		add(jLabel2, gridBagConstraints);

		jLabel3.setText("First Name" + ":");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
		add(jLabel3, gridBagConstraints);

		jLabel4.setText("Last Name" + ":");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 3;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
		add(jLabel4, gridBagConstraints);

		jLabel9.setText("Password" + ":");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 4;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
		add(jLabel9, gridBagConstraints);

		jLabel10.setText("Confirm Password" + ":");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 5;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
		add(jLabel10, gridBagConstraints);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 4;
		gridBagConstraints.gridwidth = 4;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.ipadx = 1;
		gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
		add(tfPassword1, gridBagConstraints);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 5;
		gridBagConstraints.gridwidth = 4;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.ipadx = 1;
		gridBagConstraints.insets = new java.awt.Insets(0, 5, 4, 0);
		add(tfPassword2, gridBagConstraints);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.gridwidth = 4;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.ipadx = 100;
		gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
		add(tfId, gridBagConstraints);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.gridwidth = 4;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.ipadx = 100;
		gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
		add(tfSsn, gridBagConstraints);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.gridwidth = 6;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.ipadx = 200;
		gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
		add(tfFirstName, gridBagConstraints);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 3;
		gridBagConstraints.gridwidth = 6;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.ipadx = 200;
		gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
		add(tfLastName, gridBagConstraints);

		jLabel5.setText("Cost Per Hour" + ":");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 6;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
		add(jLabel5, gridBagConstraints);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 6;
		gridBagConstraints.gridwidth = 4;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
		add(tfCostPerHour, gridBagConstraints);

		jLabel6.setText("User Type" + ":");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 7;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		add(jLabel6, gridBagConstraints);

		cbUserType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Manager", "Cashier", "Server" }));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 7;
		gridBagConstraints.gridwidth = 4;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
		add(cbUserType, gridBagConstraints);
	}// </editor-fold>//GEN-END:initComponents

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JComboBox cbUserType;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel10;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel9;
	private javax.swing.JTextField tfCostPerHour;
	private javax.swing.JTextField tfFirstName;
	private javax.swing.JTextField tfId;
	private javax.swing.JTextField tfLastName;
	private javax.swing.JPasswordField tfPassword1;
	private javax.swing.JPasswordField tfPassword2;
	private javax.swing.JTextField tfSsn;

	// End of variables declaration//GEN-END:variables
	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public String getDisplayText() {
		if (isEditMode())
			return "Edit User";

		return "Add new user";
	}

	private boolean editMode;

	@Override
	public boolean save() {
		try {
			updateModel();
		} catch (IllegalModelStateException e) {
			POSMessageDialog.showError(this, e.getMessage());
			return false;
		}

		User user = (User) getBean();
		UserDAO userDAO = UserDAO.getInstance();

		if (!editMode) {
			User user2 = userDAO.findUser(user.getUserId(), user.getNewUserType());
			if (user2 != null) {
				POSMessageDialog.showError(this, "User with ID: " + user.getUserId() + " and Type: " + user.getNewUserType() + " already exists.");
				return false;
			}
		}

		try {
			userDAO.saveOrUpdate(user, editMode);
		} catch (PosException x) {
			POSMessageDialog.showError(this, x.getMessage(), x);
			x.printStackTrace();
			return false;
		} catch (Exception x) {
			POSMessageDialog.showError(this, "Could not save user", x);
			x.printStackTrace();
			return false;
		}

		return true;
	}

	@Override
	protected boolean updateModel() throws IllegalModelStateException {
		User user = null;
		if (!(getBean() instanceof User)) {
			user = new User();
		}
		else {
			user = (User) getBean();
		}

		int id = 1000;
		try {
			id = Integer.parseInt(tfId.getText());
		} catch (Exception x) {
			throw new IllegalModelStateException("ID is not valid");
		}

		String ssn = tfSsn.getText();
		String firstName = tfFirstName.getText();
		String lastName = tfLastName.getText();
		String password1 = new String(tfPassword1.getPassword());
		String password2 = new String(tfPassword2.getPassword());

		if (POSUtil.isBlankOrNull(ssn)) {
			throw new IllegalModelStateException("SSN cannot be empty");
		}
		if (POSUtil.isBlankOrNull(firstName)) {
			throw new IllegalModelStateException("First name cannot be empty");
		}
		if (POSUtil.isBlankOrNull(lastName)) {
			throw new IllegalModelStateException("Last name cannot be empty");
		}
		if (POSUtil.isBlankOrNull(password1)) {
			throw new IllegalModelStateException("Password1 cannot be empty");
		}
		if (POSUtil.isBlankOrNull(password2)) {
			throw new IllegalModelStateException("Password2 cannot be empty");
		}
		if (!password1.equals(password2)) {
			throw new IllegalModelStateException("Password did not match");
		}

		double cost = 0;

		try {
			cost = Double.parseDouble(tfCostPerHour.getText());
		} catch (Exception x) {
			throw new IllegalModelStateException("Cost per hour for " + firstName + " " + lastName + " is not valid.");
		}

		user.setNewUserType((UserType) cbUserType.getSelectedItem());
		user.setCostPerHour(cost);

		user.setSsn(ssn);
		user.setUserId(id);
		user.setFirstName(firstName);
		user.setLastName(lastName);
		user.setPassword(password1);

		setBean(user);
		return true;
	}

	@Override
	protected void updateView() {
		if (!(getBean() instanceof User)) {
			return;
		}
		User user = (User) getBean();
		setData(user);
	}

	private void setData(User data) {
		if (data.getUserId() != null) {
			tfId.setText(String.valueOf(data.getUserId()));
		}
		else {
			tfId.setText("");
		}
		if (data.getSsn() != null) {
			tfSsn.setText(data.getSsn());
		}
		else {
			tfSsn.setText("");
		}
		tfFirstName.setText(data.getFirstName());
		tfLastName.setText(data.getLastName());
		//tfCostPerHour.setText(data.getCostPerHour() == null ? "" : data.getCostPerHour().toString());
		//cbUserType.setSelectedItem(data.getUserType());
		tfPassword1.setText(data.getPassword());
		tfPassword2.setText(data.getPassword());

		cbUserType.setSelectedItem(data.getNewUserType());
		tfCostPerHour.setText(data.getCostPerHour() == null ? "" : data.getCostPerHour().toString());

	}

	public boolean isEditMode() {
		return editMode;
	}

	public void setEditMode(boolean editMode) {
		this.editMode = editMode;
		if (editMode) {
			tfId.setEditable(false);
		}
		else {
			tfId.setEditable(true);
		}
	}

	public void setId(Integer id) {
		if (id != null) {
			tfId.setText(String.valueOf(id.intValue()));
		}
	}
}
